package com.yawn;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Attachment;
import org.activiti.engine.task.Task;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

/**
 * @author Created by yawn on 2017-12-10 15:04
 */
public class TestTaskAttachment {

    public static void main(String[] args) throws FileNotFoundException {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        RepositoryService repositoryService = engine.getRepositoryService();
        RuntimeService runtimeService = engine.getRuntimeService();
        TaskService taskService = engine.getTaskService();

        Deployment deployment = repositoryService.createDeployment().addClasspathResource("demo5.bpmn").deploy();

        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();

        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());

        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();

        // 创建附件attachment
        Attachment attachment1 = taskService.createAttachment("图片", task.getId(),
                processInstance.getId(), "headPicture", "头像",
                "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");
        Attachment attachment2 = taskService.createAttachment("文本", task.getId(),
                processInstance.getId(), "vacationReason", "请假事由",
                new FileInputStream(new File("target/classes/reason.txt")));

        // 获得attachment
        List<Attachment> processInstanceAttachments = taskService.getProcessInstanceAttachments(processInstance.getId());
        Attachment attachment = taskService.getAttachment(attachment1.getId());
        List<Attachment> taskAttachments = taskService.getTaskAttachments(task.getId());

        // attachment
        taskAttachments.forEach(attach -> {
            System.out.println(attach.getId() + "--" + attach.getName() + "--" + attach.getDescription());
            ///
//            InputStream attachmentContent = taskService.getAttachmentContent(attachment.getId());
        });


        engine.close();
    }
}
